package com.ellricky.baselib.ui.order

import android.view.View
import android.widget.AdapterView
import androidx.recyclerview.widget.RecyclerView
import com.andr.common.ui.activity.BaseToolBarActivity
import com.andr.common.ui.mvp.BasePresenter
import com.andr.common.widget.refresh.RvRefreshListener
import com.ellricky.baselib.R
import com.ellricky.baselib.cache.LocalCacheUtil
import com.ellricky.baselib.dialog.picker.EPickerUtil
import com.ellricky.baselib.entity.BaseData
import com.ellricky.baselib.entity.RenewalInfo
import com.ellricky.baselib.http.HttpUtils
import com.ellricky.baselib.util.EAppUtil
import com.ellricky.baselib.util.NewEBaseCallback
import com.ellricky.baselib.util.NewEListCallBack2
import com.ellricky.baselib.util.TimeUtils
import kotlinx.android.synthetic.main.activity_renewal_manage.*
import kotlinx.android.synthetic.main.base_fragment_active_doc_child.rvView
import java.util.*

/**
 * 续费管理页面
 */
class RenewalManageActivity : BaseToolBarActivity<Void, BasePresenter<Void>>(), RvRefreshListener,
    AdapterView.OnItemClickListener {

    private var paramMap: HashMap<String, String> = hashMapOf()
    private var dataSource: List<RenewalInfo> = arrayListOf()
    private var refresh = false
    private var ePickerUtil: EPickerUtil? = null

    private val date: String by lazy {
        TimeUtils.date2String(Date(), TimeUtils.FORMAT_MONTH)
    }

    private var paramsDate: String = TimeUtils.date2String(Date(), TimeUtils.FORMAT_DIVER_MONTH)

    private var empEid = LocalCacheUtil.getUid()

    private val adapter by lazy {
        RenewalAdapter(this)
    }

    override fun getContentLayout(): Int {
        return R.layout.activity_renewal_manage
    }

    override fun initUIView() {
        super.initUIView()

        mTvTitle.text = "续费管理"
        ePickerUtil = EPickerUtil(this)
        rvView.init(this)
        loadDataOnInit()

        //选择人员
        if (LocalCacheUtil.isPresidentRole()) {
            tv_teacher?.setOnClickListener {

                ePickerUtil?.checkStaffTypePicker(null) { _, entry ->
                    tv_teacher.text = entry.name
                    empEid = entry.id
                    loadDataOnInit()
                }
            }
        } else {
            tv_teacher.text = LocalCacheUtil.readLocalUser().U_Name
            empEid = LocalCacheUtil.readLocalUser().id.toString()
        }

        //选择时间
        tv_date?.setOnClickListener {
            //格式化日历 yyyy-mm
            ePickerUtil?.showMonthMnPicker("") { content,
                                                 _,
                                                 _ ->
                tv_date.text = TimeUtils.convertFormat(
                    content,
                    TimeUtils.FORMAT_DIVER_MONTH,
                    TimeUtils.FORMAT_MONTH
                )
                paramsDate = content
                loadDataOnInit()
            }
        }

        tv_date.text = date
    }

    private fun loadDataOnInit() {
        setRightBtn(0)
        rvView.loadContent(true)
    }

    private fun initParamMap() {
        paramMap["action"] = "renewal"
        paramMap["eid"] = LocalCacheUtil.getUid()
        paramMap["school_id"] = LocalCacheUtil.getSchoolId()
        paramMap["page"] = rvView.curPage.toString()
        paramMap["month"] = paramsDate
        paramMap["emp_type"] = getEmpType()
        paramMap["emp_id"] = empEid //被查看人id
        paramMap["page_size"] = "40"
    }

    //服务端约定
    private fun getEmpType(): String {
        when {
            LocalCacheUtil.isPresidentRole() -> {
                return "0"
            }
            LocalCacheUtil.isTmRole() -> {
                return "1"
            }
            LocalCacheUtil.isTcRole() -> {
                return "2"
            }
            LocalCacheUtil.isConsultantRole() -> {
                return "3"
            }
            LocalCacheUtil.isMarketRole() -> {
                return "4"
            }
            else -> return ""
        }
    }

    override fun setAdapter(rv: RecyclerView?) {
        val adapter = adapter
        adapter.setOnItemClickListener(this)
        rv?.adapter = adapter
    }

    override fun loadData(refresh: Boolean) {
        this.refresh = refresh
        val url = EAppUtil.getApiHost()
        initParamMap()
        val callback: NewEBaseCallback = getResponseCallback2()
        HttpUtils.getInstance().postFrom(url, paramMap, callback)
    }

    private fun getResponseCallback2(): NewEBaseCallback {
        return object : NewEListCallBack2<RenewalInfo>(this) {
            override fun onSuccess(
                data: MutableList<RenewalInfo>,
                baseData: BaseData<RenewalInfo>,
                msg: String?
            ) {
                dataSource = data
                rvView.onLoadFinish(data, refresh)
                tv_person.text = "订单笔数：${baseData.allcount}笔"
            }
        }
    }

    override fun updateAdapterData(refresh: Boolean) {
        if (refresh) adapter.refresh(dataSource)
        else adapter.loadMore(dataSource)
    }

    override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {

    }
}